﻿using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Server.BasicsDatumServer.CarrierManagementService.Dto;
using SmartWMS.Server.BasicsDatumServer.WarehouseManagementService.Dto;
using SmartWMS.Server.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.BasicsDatumServer.CarrierManagementService
{
	/// <summary>
	/// 承运商管理
	/// </summary>
	public class CarrierManagementService : ICarrierManagementService
	{
		private readonly IRepository<Carrier> _carrierResponse;

		public CarrierManagementService(IRepository<Carrier> carrierResponse)
		{
			_carrierResponse = carrierResponse;
		}

		/// <summary>
		/// 显示承运商信息
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		public async Task<(int pageCount, int totalCount, Response<List<CarrierDto>> data)> GetCarrierManagementDtoAsync(SearchCarrierDto dto)
		{
			try
			{
				var list = await _carrierResponse.GetAllListAsync();

				if (!string.IsNullOrEmpty(dto.CarrierCode))
				{
					list = list.Where(x => x.CarrierCode.Contains(dto.CarrierCode)).ToList();
				}

				if (!string.IsNullOrEmpty(dto.CarrierName))
				{
					list = list.Where(x => x.CarrierName.Contains(dto.CarrierName)).ToList();
				}

				//计算总页数
				int totalCount = list.Count();
				int pageCount = (int)Math.Ceiling(totalCount * 1.0 / dto.pageSize);
				list = list.Skip((dto.pageIndex - 1) * dto.pageSize).Take(dto.pageSize).ToList();

				var result = list.MapToList<CarrierDto>();
				return (pageCount, totalCount, Response<List<CarrierDto>>.Ok(result));
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 添加承运商
		/// </summary>
		/// <param name="info">要添加的承运商表实体</param>
		/// <returns></returns>
		public async Task<Response<bool>> AddCarrierManagement(CarrierDto info)
		{
			try
			{
				var add = info.MapTo<Carrier>();
				var result = await _carrierResponse.InsertAsync(add);
				return result > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 删除承运商管理
		/// </summary>
		/// <param name="info">要删除的承运商管理</param>
		/// <returns></returns>
		public async Task<Response<bool>> DelCarrierManagement(CarrierDto info)
		{
			try
			{
				var result = info.MapTo<Carrier>();
				var del = await _carrierResponse.DeleteByIdAsync(result);
				return del > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 反填承运商管理
		/// </summary>
		/// <param name="id">要反填的承运商Id</param>
		/// <returns></returns>
		public Response<CarrierDto> FilCarrierManagement(int id)
		{
			try
			{
				var list = _carrierResponse.GetListQuery().FirstOrDefault(x => x.Id == id);
				var result = list.MapTo<CarrierDto>();
				return Response<CarrierDto>.Ok(result);
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 修改承运商管理
		/// </summary>
		/// <param name="info">要修改的承运商实体</param>
		/// <returns></returns>
		public async Task<Response<bool>> UpdCarrierManagement(CarrierDto info)
		{
			try
			{
				var list = info.MapTo<Carrier>();
				var result = await _carrierResponse.UpdateAsync(list);
				return result > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 批量删除承运商
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		public Task<Response<bool>> BatchCarrierManagement(CarrierDto dto)
		{
			try
			{
				var sid = dto.DelId.Split(',');
				var list = new List<Carrier>();
				foreach (var item in sid)
				{
					var carrierResponseList = _carrierResponse.GetListQuery().Where(x => x.Id == int.Parse(item)).ToList();
					list.AddRange(carrierResponseList);
				}

				var result = _carrierResponse.DeleteAsync(list);
				return result.Result > 0 ? Task.FromResult(Response<bool>.Ok(result.Result > 0)) : Task.FromResult(Response<bool>.Fail(result.Result > 0));
			}
			catch (Exception)
			{
				throw;
			}
		}
	}
}
